import Watcher from "./observe/watcher";
import { patch } from "./vdom/patch";

export function mountComponent (vm) {
  const updateComponent = () => {
    vm._update(vm._render())
  }

  // true 标识是一个渲染watcher
  new Watcher(vm, updateComponent, true)
}

export function lifecycleMixin (Vue) {
  Vue.prototype._update = function (vnode) {
    const vm = this
    const el = vm.$el

    const prevVnode = vm._vnode
    vm._vnode = vnode // 把组件第一次产生的虚拟节点保存到_vnode上
    if (prevVnode) {
      vm.$el = patch(prevVnode, vnode)
    } else {
      vm.$el = patch(vm.$el, vnode)
    }
  }
}

// 调用钩子函数
export function callHook (vm, hook) {
  const handlers = vm.$options[hook]
  if (handlers) {
    handlers.forEach(handler => handler.call(vm))
  }
}